home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / EasyPHP-2.0b1-setup.exe / {app} / phpmyadmin / db_operations.php < prev    next >
Encoding:
PHP Script  |  2006-11-18  |  17.2 KB  |  489 lines

  1. <?php
  2. /* $Id: db_operations.php 9583 2006-10-18 12:38:36Z lem9 $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * handles miscellaneous db operations:
  7.  *  - move/rename
  8.  *  - copy
  9.  *  - changing collation
  10.  *  - changing comment
  11.  *  - adding tables
  12.  *  - viewing PDF schemas
  13.  */
  14.  
  15. /**
  16.  * requirements
  17.  */
  18. require_once './libraries/common.lib.php';
  19. require_once './libraries/Table.class.php';
  20. require_once './libraries/mysql_charsets.lib.php';
  21.  
  22. /**
  23.  * Rename/move or copy database
  24.  */
  25. if (isset($db) &&
  26.     ((isset($db_rename) && $db_rename == 'true') ||
  27.     (isset($db_copy) && $db_copy == 'true'))) {
  28.  
  29.     if (isset($db_rename) && $db_rename == 'true') {
  30.         $move = true;
  31.     } else {
  32.         $move = false;
  33.     }
  34.  
  35.     if (!isset($newname) || !strlen($newname)) {
  36.         $message = $strDatabaseEmpty;
  37.     } else {
  38.         $sql_query = ''; // in case target db exists
  39.         if ($move ||
  40.            (isset($create_database_before_copying) && $create_database_before_copying)) {
  41.             $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
  42.             if (isset($db_collation)) {
  43.                 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
  44.             }
  45.             $local_query .= ';';
  46.             $sql_query = $local_query;
  47.             PMA_DBI_query($local_query);
  48.         }
  49.  
  50.         if (isset($GLOBALS['add_constraints'])) {
  51.             $GLOBALS['sql_constraints_query_full_db'] = '';
  52.         }
  53.  
  54.         $tables_full = PMA_DBI_get_tables_full($db);
  55.         $views = array();
  56.         foreach ($tables_full as $table => $tmp) {
  57.             // to be able to rename a db containing views, we
  58.             // first collect in $views all the views we find and we
  59.             // will handle them after the tables
  60.             /** 
  61.              * @todo support a view of a view 
  62.              */
  63.             if (PMA_Table::isView($db, $table)) {
  64.                 $views[] = $table;
  65.                 continue;
  66.             }
  67.  
  68.             $back = $sql_query;
  69.             $sql_query = '';
  70.  
  71.             // value of $what for this table only
  72.             $this_what = $what;
  73.  
  74.             if (!isset($tables_full[$table]['Engine'])) {
  75.                 $tables_full[$table]['Engine'] = $tables_full[$table]['Type'];
  76.             }
  77.             // do not copy the data from a Merge table
  78.             // note: on the calling FORM, 'data' means 'structure and data'
  79.             if ($tables_full[$table]['Engine'] == 'MRG_MyISAM') {
  80.                 if ($this_what == 'data') {
  81.                     $this_what = 'structure';
  82.                 }
  83.                 if ($this_what == 'dataonly') {
  84.                     $this_what = 'nocopy';
  85.                 }
  86.             }
  87.  
  88.             if ($this_what != 'nocopy') {
  89.                 PMA_Table::moveCopy($db, $table, $newname, $table,
  90.                     isset($this_what) ? $this_what : 'data', $move, 'db_copy');
  91.                 if (isset($GLOBALS['add_constraints'])) {
  92.                     $GLOBALS['sql_constraints_query_full_db'] .= $GLOBALS['sql_constraints_query'];
  93.                     unset($GLOBALS['sql_constraints_query']);
  94.                 }
  95.             }
  96.  
  97.             $sql_query = $back . $sql_query;
  98.         } // end (foreach)
  99.         unset($table);
  100.  
  101.         // handle the views
  102.         foreach ($views as $view) {
  103.             PMA_Table::moveCopy($db, $view, $newname, $view,
  104.                 'structure', $move, 'db_copy');
  105.         }
  106.         unset($view, $views);
  107.         
  108.         // now that all tables exist, create all the accumulated constraints 
  109.         if (isset($GLOBALS['add_constraints'])) {
  110.             /**
  111.              * @todo this works with mysqli but not with mysql, because
  112.              * mysql extension does not accept more than one statement; maybe
  113.              * interface with the sql import plugin that handles statement delimiter
  114.              */
  115.             PMA_DBI_query($GLOBALS['sql_constraints_query_full_db']);
  116.  
  117.             // and prepare to display them
  118.             $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query_full_db'];
  119.             unset($GLOBALS['sql_constraints_query_full_db']);
  120.         }
  121.  
  122.         // Duplicate the bookmarks for this db (done once for each db)
  123.         if ($db != $newname) {
  124.             $get_fields = array('user', 'label', 'query');
  125.             $where_fields = array('dbase' => $db);
  126.             $new_fields = array('dbase' => $newname);
  127.             PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
  128.                 $where_fields, $new_fields);
  129.         }
  130.  
  131.         if ($move) {
  132.             // cleanup pmadb stuff for this db
  133.             require_once './libraries/relation_cleanup.lib.php';
  134.             PMA_relationsCleanupDatabase($db);
  135.  
  136.             $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
  137.             $sql_query .= "\n" . $local_query;
  138.             PMA_DBI_query($local_query);
  139.             $message    = sprintf($strRenameDatabaseOK, htmlspecialchars($db),
  140.                 htmlspecialchars($newname));
  141.         } else {
  142.             $message    = sprintf($strCopyDatabaseOK, htmlspecialchars($db),
  143.                 htmlspecialchars($newname));
  144.         }
  145.         $reload     = true;
  146.  
  147.         /* Change database to be used */
  148.         if ($move) {
  149.             $db         = $newname;
  150.         } else {
  151.             if (isset($switch_to_new) && $switch_to_new == 'true') {
  152.                 PMA_setCookie('pma_switch_to_new', 'true');
  153.                 $db         = $newname;
  154.             } else {
  155.                 PMA_setCookie('pma_switch_to_new', '');
  156.             }
  157.         }
  158.     }
  159. }
  160. /**
  161.  * Settings for relations stuff
  162.  */
  163.  
  164. require_once './libraries/relation.lib.php';
  165. $cfgRelation = PMA_getRelationsParam();
  166.  
  167. /**
  168.  * Check if comments were updated
  169.  * (must be done before displaying the menu tabs)
  170.  */
  171. if ($cfgRelation['commwork'] && isset($db_comment) && $db_comment == 'true') {
  172.     PMA_SetComment($db, '', '(db_comment)', $comment);
  173. }
  174.  
  175. /**
  176.  * Prepares the tables list if the user where not redirected to this script
  177.  * because there is no table in the database ($is_info is true)
  178.  */
  179. if (empty($is_info)) {
  180.     require './libraries/db_details_common.inc.php';
  181.     $url_query .= '&goto=db_operations.php';
  182.  
  183.     // Gets the database structure
  184.     $sub_part = '_structure';
  185.     require './libraries/db_details_db_info.inc.php';
  186.     echo "\n";
  187. }
  188.  
  189. if (PMA_MYSQL_INT_VERSION >= 40101) {
  190.     $db_collation = PMA_getDbCollation($db);
  191. }
  192. if (PMA_MYSQL_INT_VERSION < 50002
  193.   || (PMA_MYSQL_INT_VERSION >= 50002 && $db != 'information_schema')) {
  194.     $is_information_schema = false;
  195. } else {
  196.     $is_information_schema = true;
  197. }
  198.  
  199. if (!$is_information_schema) {
  200.  
  201.     require './libraries/display_create_table.lib.php';
  202.  
  203.     if ($cfgRelation['commwork']) {
  204.         /**
  205.          * database comment
  206.          */
  207.         ?>
  208.     <form method="post" action="db_operations.php">
  209.     <?php echo PMA_generate_common_hidden_inputs($db); ?>
  210.     <input type="hidden" name="db_comment" value="true" />
  211.     <fieldset>
  212.         <legend>
  213.         <?php
  214.         if ($cfg['PropertiesIconic']) {
  215.             echo '<img class="icon" src="' . $pmaThemeImage . 'b_comment.png"'
  216.                 .' alt="" border="0" width="16" height="16" hspace="2" align="middle" />';
  217.         }
  218.         echo $strDBComment;
  219.         $comment = PMA_getComments($db);
  220.         ?>
  221.         </legend>
  222.         <input type="text" name="comment" class="textfield" size="30"
  223.             value="<?php
  224.             echo (isset($comment) && is_array($comment)
  225.                 ? htmlspecialchars(implode(' ', $comment))
  226.                 : ''); ?>" />
  227.         <input type="submit" value="<?php echo $strGo; ?>" />
  228.     </fieldset>
  229.     </form>
  230.         <?php
  231.     }
  232.     /**
  233.      * rename database
  234.      */
  235.     ?>
  236.     <form method="post" action="db_operations.php"
  237.         onsubmit="return emptyFormElements(this, 'newname')">
  238.     <input type="hidden" name="what" value="data" />
  239.     <input type="hidden" name="db_rename" value="true" />
  240.     <?php echo PMA_generate_common_hidden_inputs($db); ?>
  241.     <fieldset>
  242.         <legend>
  243.     <?php
  244.     if ($cfg['PropertiesIconic']) {
  245.         echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
  246.             .' alt="" width="16" height="16" />';
  247.     }
  248.     echo $strDBRename . ':';
  249.     ?>
  250.         </legend>
  251.         <input type="text" name="newname" size="30" class="textfield" value="" />
  252.         <input type="submit" value="<?php echo $strGo; ?>" />
  253.     </fieldset>
  254.     </form>
  255.  
  256.     <?php
  257.     /**
  258.      * Copy database
  259.      */
  260.     ?>
  261.     <form method="post" action="db_operations.php"
  262.         onsubmit="return emptyFormElements(this, 'newname')">
  263.     <?php
  264.     if (isset($db_collation)) {
  265.         echo '<input type="hidden" name="db_collation" value="' . $db_collation
  266.             .'" />' . "\n";
  267.     }
  268.     echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
  269.     echo PMA_generate_common_hidden_inputs($db);
  270.     ?>
  271.     <fieldset>
  272.         <legend>
  273.     <?php
  274.     if ($cfg['PropertiesIconic']) {
  275.         echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
  276.             .' alt="" width="16" height="16" />';
  277.     }
  278.     echo $strDBCopy . ':';
  279.     if (PMA_MYSQL_INT_VERSION >= 50000) {
  280.         $drop_clause = 'DROP TABLE / DROP VIEW';
  281.     } else {
  282.         $drop_clause = 'DROP TABLE';
  283.     }
  284.     ?>
  285.         </legend>
  286.         <input type="text" name="newname" size="30" class="textfield" value="" /><br />
  287.         <input type="radio" name="what" value="structure"
  288.             id="radio_copy_structure" style="vertical-align: middle" />
  289.         <label for="radio_copy_structure"><?php echo $strStrucOnly; ?></label><br />
  290.         <input type="radio" name="what" value="data" id="radio_copy_data"
  291.             checked="checked" style="vertical-align: middle" />
  292.         <label for="radio_copy_data"><?php echo $strStrucData; ?></label><br />
  293.         <input type="radio" name="what" value="dataonly"
  294.             id="radio_copy_dataonly" style="vertical-align: middle" />
  295.         <label for="radio_copy_dataonly"><?php echo $strDataOnly; ?></label><br />
  296.  
  297.         <input type="checkbox" name="create_database_before_copying" value="1"
  298.             id="checkbox_create_database_before_copying"
  299.             style="vertical-align: middle" checked="checked" />
  300.         <label for="checkbox_create_database_before_copying">
  301.             <?php echo $strCreateDatabaseBeforeCopying; ?></label><br />
  302.         <input type="checkbox" name="drop_if_exists" value="true"
  303.             id="checkbox_drop" style="vertical-align: middle" />
  304.         <label for="checkbox_drop"><?php echo sprintf($strAddClause, $drop_clause); ?></label><br />
  305.         <input type="checkbox" name="sql_auto_increment" value="1"
  306.             id="checkbox_auto_increment" style="vertical-align: middle" />
  307.         <label for="checkbox_auto_increment">
  308.             <?php echo $strAddAutoIncrement; ?></label><br />
  309.         <input type="checkbox" name="add_constraints" value="1"
  310.             id="checkbox_constraints" style="vertical-align: middle" />
  311.         <label for="checkbox_constraints">
  312.             <?php echo $strAddConstraints; ?></label><br />
  313.     <?php
  314.     unset($drop_clause);
  315.  
  316.     if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
  317.       && $_COOKIE['pma_switch_to_new'] == 'true') {
  318.         $pma_switch_to_new = 'true';
  319.     }
  320.     ?>
  321.         <input type="checkbox" name="switch_to_new" value="true"
  322.             id="checkbox_switch"
  323.             <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
  324.             style="vertical-align: middle" />
  325.         <label for="checkbox_switch"><?php echo $strSwitchToDatabase; ?></label>
  326.     </fieldset>
  327.     <fieldset class="tblFooters">
  328.         <input type="submit" name="submit_copy" value="<?php echo $strGo; ?>" />
  329.     </fieldset>
  330.     </form>
  331.  
  332.     <?php
  333.     /**
  334.      * Change database charset
  335.      */
  336.     if (PMA_MYSQL_INT_VERSION >= 40101) {
  337.     // MySQL supports setting default charsets / collations for databases since
  338.     // version 4.1.1.
  339.         echo '<form method="post" action="./db_operations.php">' . "\n"
  340.            . PMA_generate_common_hidden_inputs($db, isset($table) ? $table : '')
  341.            . '<fieldset>' . "\n"
  342.            . '    <legend>';
  343.         if ($cfg['PropertiesIconic']) {
  344.             echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
  345.                 .' alt="" width="16" height="16" />';
  346.         }
  347.         echo '    <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
  348.            . '    </legend>' . "\n"
  349.            . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
  350.                 'db_collation', 'select_db_collation', $db_collation, false, 3)
  351.            . '    <input type="submit" name="submitcollation"'
  352.            . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
  353.            . '</fieldset>' . "\n"
  354.            . '</form>' . "\n";
  355.     }
  356.  
  357.     if ($num_tables > 0
  358.       && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
  359.         echo '<div class="error"><h1>' . $strError . '</h1>'
  360.             . sprintf($strRelationNotWorking,
  361.                 '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">',
  362.                 '</a>')
  363.             . '</div>';
  364.     } // end if
  365. } // end if (!$is_information_schema)
  366.  
  367.  
  368. // not sure about leaving the PDF dialog for information_schema
  369. if ($num_tables > 0 && isset($table)) {
  370.     $takeaway = $url_query . '&table=' . urlencode($table);
  371. } else {
  372.     $takeaway = '';
  373. }
  374.  
  375. if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
  376.     <!-- Work on PDF Pages -->
  377.  
  378.     <?php
  379.     // We only show this if we find something in the new pdf_pages table
  380.  
  381.     $test_query = '
  382.          SELECT *
  383.            FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
  384.           WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
  385.     $test_rs    = PMA_query_as_cu($test_query, null, PMA_DBI_QUERY_STORE);
  386.  
  387.     if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?>
  388.     <!-- PDF schema -->
  389.     <form method="post" action="pdf_schema.php">
  390.     <fieldset>
  391.         <legend>
  392.         <?php
  393.         echo PMA_generate_common_hidden_inputs($db);
  394.         if ($cfg['PropertiesIconic']) {
  395.             echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
  396.                 .' alt="" width="16" height="16" />';
  397.         }
  398.         echo $strDisplayPDF;
  399.         ?>:
  400.         </legend>
  401.         <label for="pdf_page_number_opt"><?php echo $strPageNumber; ?></label>
  402.         <select name="pdf_page_number" id="pdf_page_number_opt">
  403.         <?php
  404.         while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
  405.             echo '                <option value="' . $pages['page_nr'] . '">'
  406.                 . $pages['page_nr'] . ': ' . $pages['page_descr'] . '</option>' . "\n";
  407.         } // end while
  408.         PMA_DBI_free_result($test_rs);
  409.         unset($test_rs);
  410.         ?>
  411.         </select><br />
  412.  
  413.         <input type="checkbox" name="show_grid" id="show_grid_opt" />
  414.         <label for="show_grid_opt"><?php echo $strShowGrid; ?></label><br />
  415.         <input type="checkbox" name="show_color" id="show_color_opt"
  416.             checked="checked" />
  417.         <label for="show_color_opt"><?php echo $strShowColor; ?></label><br />
  418.         <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
  419.         <label for="show_table_dim_opt"><?php echo $strShowTableDimension; ?>
  420.             </label><br />
  421.         <input type="checkbox" name="all_tab_same_wide" id="all_tab_same_wide" />
  422.         <label for="all_tab_same_wide"><?php echo $strAllTableSameWidth; ?>
  423.             </label><br />
  424.         <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
  425.         <label for="with_doc"><?php echo $strDataDict; ?></label><br />
  426.  
  427.         <label for="orientation_opt"><?php echo $strShowDatadictAs; ?></label>
  428.         <select name="orientation" id="orientation_opt">
  429.             <option value="L"><?php echo $strLandscape;?></option>
  430.             <option value="P"><?php echo $strPortrait;?></option>
  431.         </select><br />
  432.  
  433.         <label for="paper_opt"><?php echo $strPaperSize; ?></label>
  434.         <select name="paper" id="paper_opt">
  435.         <?php
  436.             foreach ($cfg['PDFPageSizes'] AS $key => $val) {
  437.                 echo '<option value="' . $val . '"';
  438.                 if ($val == $cfg['PDFDefaultPageSize']) {
  439.                     echo ' selected="selected"';
  440.                 }
  441.                 echo ' >' . $val . '</option>' . "\n";
  442.             }
  443.         ?>
  444.         </select>
  445.     </fieldset>
  446.     <fieldset class="tblFooters">
  447.         <input type="submit" value="<?php echo $strGo; ?>" />
  448.     </fieldset>
  449.     </form>
  450.         <?php
  451.     }   // end if
  452.     ?>
  453.     <ul>
  454.         <li>
  455.     <?php
  456.         echo '<a href="pdf_pages.php?' . $takeaway . '">';
  457.         if ($cfg['PropertiesIconic']) {
  458.             echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
  459.                 .' alt="" width="16" height="16" />';
  460.         }
  461.         echo $strEditPDFPages . '</a>';
  462.     ?>
  463.         </li>
  464.     </ul>
  465.     <?php
  466. } // end if
  467.  
  468. if ($num_tables > 0
  469.   && $cfgRelation['relwork'] && $cfgRelation['commwork']
  470.   && isset($cfg['docSQLDir']) && !empty($cfg['docSQLDir'])) {
  471.     /**
  472.      * import docSQL files
  473.      */
  474.     echo '<ul>' . "\n"
  475.         .'<li><a href="db_details_importdocsql.php?' . $takeaway . '">' . "\n";
  476.     if ($cfg['PropertiesIconic']) {
  477.         echo '<img class="icon" src="' . $pmaThemeImage . 'b_docsql.png"'
  478.             .' alt="" width="16" height="16" />';
  479.     }
  480.     echo $strImportDocSQL . '</a></li>' . "\n"
  481.         .'</ul>';
  482. }
  483.  
  484. /**
  485.  * Displays the footer
  486.  */
  487. require_once './libraries/footer.inc.php';
  488. ?>
  489.